import { Directive, ElementRef, Input, Component,OnChanges } from '@angular/core';

@Directive({
  selector: 'code-formatter'
})
export class CodeFormatterDirective implements OnChanges{
  private el:HTMLElement;
  static chosenInitialized:boolean = false;
  @Input() code: string;
  @Input() type: string;
  constructor(el: ElementRef) {
    this.el = el.nativeElement;
  }

  ngOnChanges(changes) {
    var date = new Date();
    var id = "codeformatercontainer"+date.getTime();
      if(this.code!=="" && this.code!==undefined)
      {
        $(this.el).attr("id",id);
        if(this.type.toUpperCase() === "JSON")
        {
          var options = {
              dom : '#'+id, //对应容器的css选择器
              imgCollapsed: "assets/images/Collapsed.gif",
              imgExpanded: "assets/images/Expanded.gif"
          };
          var data = JSON.stringify(options);
          var jf = eval("new JsonFormater("+data+")")//创建对象
          jf.doFormat(this.code); //格式化json
        }else if(this.type === "TEXT")
        {
            $("#"+id).text(this.code);
        }

      }
  }
}
